package com.lanju.controller;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.lanju.common.SpringUtils;
import com.lanju.entity.User;
import com.lanju.service.BillsService;
import com.lanju.service.JedisService;
import com.lanju.service.UserService;

@Controller
@RequestMapping("/bills")
public class billsController {
	private static final Logger LOGGER = LoggerFactory
            .getLogger(billsController.class);
    @Autowired
    BillsService billsService;
    @Resource
   	JedisService jedisService;
    
    @Autowired
    UserService userService;
    
    @RequestMapping("/queryBillByUserId")
    @ResponseBody
    public List<Map> queryBillByUserId(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
//    	User user =(User) session.getAttribute("userModel");
//    	param.put("userId", param.get("userId").toString());
        return billsService.queryBillByUserId(param);
        
    }
    
    @RequestMapping("/queryBillsList")
    @ResponseBody
    public Map<String, Object> queryBillsList(HttpServletRequest request,HttpSession session){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
//    	User user =(User) session.getAttribute("userModel");
//    	param.put("userId", user.getUserId());
        return billsService.queryBillsList(param);
        
    }
    
    @RequestMapping("/deleteBills")
    @ResponseBody
    public Map<String, Object> deleteBills(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return billsService.deleteBills(param);
        
    }
    
    @RequestMapping("/queryDetailById")
    @ResponseBody
    public Map<String, Object> queryDetailById(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
        return billsService.queryDetailById(param);
        
    }
    /**
     * 新增单据
     * @param files
     * @param request
     * @param session
     * @return
     */
    @RequestMapping("/addBills")
    @ResponseBody
    public Map<String,Object> addBills(@RequestParam(value="titlefile",required=false) MultipartFile[] files,HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
//    	User user = SecurityContext.getUserFromSession(request);
    	User user=userService.getById(param.get("userId").toString());
    	param.put("userType", user.getFlag());
		String url=uploadImg(files); 
		param.put("imgurl", url);
    	return billsService.addBills(param);
    }

    @RequestMapping("/updateBills")
    @ResponseBody
    public Map<String,Object> updateBills(@RequestParam(value="titlefile",required=false) MultipartFile[] files,HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
//    	 User user = SecurityContext.getUserFromSession(request);
    	User user=userService.getById(param.get("userId").toString());
		String url=uploadImg(files); 
		param.put("imgurl", url);
    	return billsService.updateBills(param);
    }
   
    @RequestMapping("/getIdCard")
    @ResponseBody
    public Map<String,Object> getIdCard(HttpServletRequest request){
    	Map<String, Object> param = SpringUtils.getParameterMap(request);
		return billsService.getIdCard(param);
    }
	private String uploadImg(MultipartFile[] files) {
		String Url = null;
		if (files != null && files.length > 0) {
			// 循环获取file数组中得文件
			for (int i = 0; i < files.length; i++) {
		MultipartFile file = files[i];
		 OutputStream os = null;
		 InputStream in= null;
		 String picPath=null;
        try {
        	in=file.getInputStream();
   		 // 2、保存到临时文件
           // 1K的数据缓冲
           byte[] bs = new byte[1024];
           // 读取到的数据长度
           int len;
           // 输出的文件流保存到本地文件
           File tempFile = new File("/webapp/lanjufile/bills/");//linux
//             File tempFile = new File("D:/upFiles/bills/");
           if (!tempFile.exists()) {
               tempFile.mkdirs();
           }else{
           	LOGGER.info("文件已存在");
           }
//           String fullPath="D:/upFiles/bills/"+file.getOriginalFilename();
           String fullPath="/webapp/lanjufile/bills/"+file.getOriginalFilename();//linux
           System.out.println("文件全路径"+fullPath);
			os = new FileOutputStream(fullPath);
			while ((len = in.read(bs)) != -1) {
			    try {
					os.write(bs, 0, len);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			os.close();
			in.close();
		} catch (Exception e3) {
			e3.printStackTrace();
		}
       Url = "/dataResourceImages/bills/"+file.getOriginalFilename();
      
	}  
		}
		 return Url;
	}

}

